package com.sgb.knowledgeshare;

import android.util.Base64;
import androidx.lifecycle.ViewModel;
import com.sgb.knowledgeshare.entity.ShareLoginEntity;
import com.sgb.knowledgeshare.entity.UserMsgEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;
public class shareMainModel  extends ViewModel {
    ShareMainActivity activity;
    public void setActivirt(ShareMainActivity shareMainActivity) {
        this.activity = shareMainActivity;
    }

    public void pwdLoginModel(String phone, String pws) {
        ShareConfig.getInstance().getApi().login(phone, Base64.encodeToString(pws.getBytes(), Base64.DEFAULT)).enqueue(new ICallBack<BaseEntity<ShareLoginEntity>>() {
            @Override
            public void onSuccess(BaseEntity<ShareLoginEntity> data) {
                ShareLoginEntity loginData = data.getData();
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, loginData.getToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, loginData.getResourcePCToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, loginData.getResourceAPPToken());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, loginData.getResourcePCToken());
                getUserInfo(loginData);
            }
        });
    }

    public void getUserInfo(ShareLoginEntity loginData) {
        ShareConfig.getInstance().getApi().getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                //给SharedPreferenceUtil中的USER_NAME赋值 保存用户姓名
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NAME, data.getData().getUsername());
                //给SharedPreferenceUtil中的USER_NO赋值 保存用户ID
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
                //给SharedPreferenceUtil中的REAL_NAME赋值 保存实名认证的状态
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, data.getData().getRealName());
                //给SharedPreferenceUtil中的DRIVER_AUTH赋值 保存司机认证的状态
                //(旧数据)司机认证字段 @"NONE"-未认证 @"STANDBY"-认证中  @"FAIL"-认证失败 @"SUCCESS"-认证成功
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, data.getData().isDriverAuth() ? "SUCCESS" : "NONE");
                //给SharedPreferenceUtil中的DRIVER_AUTH赋值 保存企业认证的状态
                //(旧数据)NONE 未认证  SUCCESS 认证成功
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, data.getData().isRelevance() ? "SUCCESS" : "NONE");
                //保存是否关联企业 0未关联  1已关联
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.RELEVANCE, data.getData().isRelevance() ? "1" : "0");
                //保存个人认证状态[0:未认证,1:已认证]
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, data.getData().getPersonalAuth() == 1 ? true : false);
                //保存个人身份证号
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ID_CARD, data.getData().getIdentityCard());
                //当前用户：0个人，1企业，2团队
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, data.getData().getType());
                if (data.getData().getType() != 0){
                    //保存当前企业名称
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, data.getData().getCurrentIdentityName());
                    //保存当前企业logo
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMP_LOGO, data.getData().getCurrentIdentityLogo());
                    //保存当前企业No
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, data.getData().getCurrentIdentityNo());
                }else {
                    //保存当前企业名称
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, "");
                    //保存当前企业logo
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMP_LOGO, "");
                    //保存当前企业No
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, "");
                }
                //保存是否当前身份加入企业
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.IS_AUTH_OR_JOIN_COMPANY, data.getData().isRelevance());
                activity.startHomeShare();
            }
        });

    }
}
